package com.karry.yl.web.assessment;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.assessment.AssessRecords;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.domain.elder.Folk;
import com.karry.yl.service.assessment.AssessRecordsService;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.elder.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 评估记录Controller
 */
@Controller
@RequestMapping("/gbassessRecords")
public class AssessRecordsController extends SuperController {

    @Autowired
    private AssessRecordsService assessRecordsService;
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private OrganizationService organizationService;

    /**
     * 到列表页
     *
     * @return
     */
    @RequestMapping
    public String listUI() {
        return "assessment/assess";
    }
    /**
     * 到列表页
     *
     * @return
     */
    @RequestMapping("listLsUI")
    public String listLsUI() {
        return "assessment/list";
    }
    /**
     * 选择老人页面
     * @return
     */
    @RequestMapping("/select/elderly")
    public String selectElderlyUI(){
        return "assessment/selectElderly";
    }
    /**
     * 选择老人页面
     * @return
     */
    @RequestMapping("/select/selectElderly")
    public String selectElderly(){
        return "elder/elderly/selectElderly";
    }

    @RequestMapping("/select/elderly/list")
    @ResponseBody
    public LayTableResponse selectElderlyList(int page, int limit, Integer healthCondition, Integer checkedIn, String code, String name){
        Wrapper<Elderly> elderlyWrapper = new EntityWrapper<Elderly>().eq("orgId", currentUser.getOrgId()).eq("deleted",false);
        if(!StringUtils.isEmpty(healthCondition)){
            elderlyWrapper.eq("healthCondition",healthCondition);
        }
        if(!StringUtils.isEmpty(checkedIn)){
            elderlyWrapper.eq("checkedIn",checkedIn);
        }
        if(!StringUtils.isEmpty(code)){
            elderlyWrapper.like("code",code);
        }
        if(!StringUtils.isEmpty(name)){
            elderlyWrapper.like("name",name);
        }
        Page<Elderly> elderlyPage = elderlyService.selectPage(new Page<Elderly>(page, limit),elderlyWrapper);
        return new LayTableResponse(elderlyPage.getRecords(),elderlyPage.getTotal());
    }
    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit,String elderlyName,String rs) {
        Page<AssessRecords> assessRecordsPage=null;
        if(!StringUtils.isEmpty(rs)){
            assessRecordsPage=assessRecordsService.selectPage(new Page<AssessRecords>(page, limit), new EntityWrapper<AssessRecords>().eq("orgId", currentUser.getOrgId()).orderBy("createdTime",false).like("elderlyName",elderlyName).eq("rs",Integer.parseInt(rs)));
        }else{
            assessRecordsPage=assessRecordsService.selectPage(new Page<AssessRecords>(page, limit), new EntityWrapper<AssessRecords>().eq("orgId", currentUser.getOrgId()).orderBy("createdTime",false).like("elderlyName",elderlyName));
        }
        return new LayTableResponse(assessRecordsPage.getRecords(), assessRecordsPage.getTotal());
    }

    /**
     * 提交评估结果
     * @return
     */
    @RequestMapping("/submit")
    public String submit(AssessRecords assessRecords,ModelMap map){
        assessRecords.setOrgId(currentUser.getOrgId());
        assessRecords.setOrgName(organizationService.selectById(currentUser.getOrgId()).getName());
        AssessRecords records = assessRecordsService.addAssess(assessRecords);
        map.put("records",records);
        Elderly elderly=elderlyService.selectById(records.getElderlyId());
        if(records.getRs()==1){
            elderly.setPgjg("自理老人");
            elderly.setHealthCondition("1");
        }else if(records.getRs()==2){
            elderly.setPgjg("半失能老人");
            elderly.setHealthCondition("2");
        }else if(records.getRs()==3){
            elderly.setPgjg("失能老人");
            elderly.setHealthCondition("3");
        }
        elderlyService.updateById(elderly);
        List<Folk> folks = elderlyService.selectFamilyList(elderly.getId());
        map.put("elderly",elderly);
        map.put("folks",folks);
        return "assessment/result";
    }
    /**
     * 上传评估表
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public JsonResponse upload(AssessRecords assessRecords){
        AssessRecords assess=assessRecordsService.selectById(assessRecords.getId());
        assess.setFileName(assessRecords.getFileName());
        assess.setFilePath(assessRecords.getFilePath());
        assess.setAuditStatus(1);
        assessRecordsService.updateById(assess);
        return JsonResponse.success(assessRecords);
    }

    @RequestMapping("/show")
    public String show(Long arId,ModelMap map){
        AssessRecords records = assessRecordsService.selectById(arId);
        map.put("records",records);
        Elderly elderly=elderlyService.selectById(records.getElderlyId());
        List<Folk> folks = elderlyService.selectFamilyList(elderly.getId());
        map.put("elderly",elderly);
        map.put("folks",folks);
        return "assessment/show";
    }
    /**
     * 点击提交审核
     * @return
     */
    @RequestMapping("/tjshenhe")
    public String tjshenhe(Long id,ModelMap map){
        map.put("id",id);
        return "assessment/tjshenhe";
    }
}
